package org.mii.safeguard_v1.security.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.mii.safeguard_v1.base.service.ServiceException;
import org.mii.safeguard_v1.base.web.CrudActionSupport;
import org.mii.safeguard_v1.security.entity.Authority;
import org.mii.safeguard_v1.security.entity.Resource;
import org.mii.safeguard_v1.security.service.IAuthorityManager;
import org.mii.safeguard_v1.security.service.IResourceManager;
import org.springframework.beans.factory.annotation.Autowired;

@SuppressWarnings("serial")
@Results( {
		@Result(name = CrudActionSupport.RELOAD, location = "auth.action", type = "redirect"),
		@Result(name = CrudActionSupport.NOTICE, location = "notice.jsp", type = "redirect"),
		@Result(name = CrudActionSupport.DENY_ACCESS, location = "common/403.jsp", type = "redirect") })
@Namespace("/security")
public class AuthAction extends CrudActionSupport<Authority> {

	@Autowired
	IAuthorityManager authorityManager;

	@Autowired
	IResourceManager resourceManager;

	// 基本属性
	private Authority entity;
	private Integer id;
	private List<Authority> allAuths;

	// 资源相关属性
	private List<Resource> allRescs;

	@Override
	public String delete() throws Exception {
		try {
			authorityManager.delete(id);
			addActionMessage("删除权限成功");
		} catch (ServiceException e) {
			logger.error(e.getMessage(), e);
			addActionMessage(e.getMessage());
		}
		return RELOAD;
	}

	@Override
	public String list() throws Exception {
		allAuths = authorityManager.getAll();
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		allRescs = resourceManager.getAll();
		// checkedAuthIds = entity.getAuthIds();
		return INPUT;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = authorityManager.get(id);
		} else {
			entity = new Authority();
		}
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Override
	public String save() throws Exception {
		authorityManager.save(entity);
		addActionMessage("保存权限成功");
		return RELOAD;
	}

	public Authority getModel() {
		return entity;
	}

	public List<Authority> getAllAuths() {
		return allAuths;
	}

	public List<Resource> getAllRescs() {
		return allRescs;
	}

}
